#pragma once
#include<assert.h>
namespace bit
{
	template<class T>
	struct list_node
	{
		list_node<T> next;
		list_node<T> ptr;
		T vue;
		list_node<T>(const T& v) :
			next(nullptr), ptr(nullptr), vue(v)
		{}
	};
	// typedef __list_iterator<T, const T&, const T*> const_iterator;
	template<class T,class Ref,class Ptr>
	struct _list_iterator
	{
		typedef list_node<T> Node;
		typedef _list_iterator<T, Ref, Ptr> self;
		Node* node;
		_list_iterator(Node* _node):
			node(_node)
		{}
		Ref operator*()
		{
			return node->vue;
		}
		Ptr operator->()
		{
			return &node->vue;
		}
		self& operator++()
		{
			node = node->next;
			return *this;
		}
		self operator++(int)
		{
			self tem(this);
			node = node->next;
			return tem;
		}
		self& operator--()
		{
			node = node->ptr;
			return *this;
		}
		self operator--(int)
		{
			self tem(this);
			node = node->ptr;
			return tem;
		}
		bool operator==(Node* _node)
		{
			if (_node->next == node->next && _node->ptr == node->ptr && _node->vue == node->vue)
				return true;
			else
				return false;
		}
		bool operator!=(Node* _node)
		{
			if (node == _node)
				return false;
			else
				return true;
		}
	};
	template<class T>
	class list
	{
		typedef list_node<T> Node;
	public:
		typedef _list_iterator<T, T&, T*> iterator;
		typedef _list_iterator<T, const T&, const T*> const_iterator;
		iterator begin()
		{
			return iterator( head->next);
		}
		iterator end()
		{
			return iterator(head);
		}
		const_iterator begin()const
		{
			return const_iterator(head->next);
		}
		const_iterator end()const
		{
			return const_iterator(head);
		}
		void enpty_init()
		{
			head = new Node;
			head->next = head;
			head->ptr = head;
			size = 0;
		}
		iterator insert(iterator pos, const T& x)
		{
			Node* newnode = new Node;
			newnode->vue = x;
			newnode->next = pos.node->next;
			newnode->ptr = pos;
			pos.node->next.ptr = newnode;
			pos.node->next = newnode;
			this->size++;
			return newnode;
		}
		iterator erase(iterator pos)
		{
			Node* it = pos.node->next;
			pos.node->ptr.next = pos.node->next;
			pos.node->next.ptr = pos.node->ptr;
			pos.node->next = nullptr;
			pos.node->ptr = nullptr;
			delete[] pos;
			size--;
			return it;
		}
		size_t Size()
		{
			return *this->size;
		}
		void push_back(const T& x)
		{
			insert(end(), x);
		}

		void push_front(const T& x)
		{
			insert(begin(), x);
		}

		void pop_back()
		{
			erase(--end());
		}

		void pop_front()
		{
			erase(begin());
		}
		list<T> operator=(const list<T> v)
		{
			std::swap(this.head, v.head);
			std::swap(this.size, v.size);
			return *this;
		}
		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
			size = 0;
		}
		~list()
		{
			clear();
			delete head;
			head = nullptr;
		}
		list(const list<T>& lt)
		{
			this->enpty_init();
			for (auto e : lt)
			{
				this->push_back(e);
			}
		}
		list()
		{
			enpty_init();
		}
	public:
		Node* head;
		size_t size;
	};
}
void Print(const list<int>& lt)
{
	list<int>::const_iterator it = lt.begin();
	while (it != lt.end())
	{
		cout << *it << " ";
		it++;
	}
}
void test_list1()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);

	list<int>::iterator it = lt.begin();

	while (it != lt.end())
	{
		(*it) += 1;
		cout << *it << " ";
		++it;
	}
	cout << endl;

	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

	Print(lt);
}

struct A
{
	A(int a1 = 0, int a2 = 0)
		:_a1(a1)
		, _a2(a2)
	{}

	int _a1;
	int _a2;
};

void test_list2()
{
	list<A> lt;
	lt.push_back(A(1, 1));
	lt.push_back(A(2, 2));
	lt.push_back(A(3, 3));
	lt.push_back(A(4, 4));

	list<A>::iterator it = lt.begin();
	while (it != lt.end())
	{
		//cout << (*it)._a1 << " " << (*it)._a2 << endl;
		cout << it->_a1 << " " << it->_a2 << endl;

		++it;
	}
	cout << endl;
}

void test_list3()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	lt.push_front(5);
	lt.push_front(6);
	lt.push_front(7);
	lt.push_front(8);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

	lt.pop_front();
	lt.pop_back();

	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

	lt.clear();
	lt.push_back(10);
	lt.push_back(20);
	lt.push_back(30);
	lt.push_back(40);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

	cout << lt.size() << endl;
}

void test_list4()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);

	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

	list<int> lt1(lt);
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;

	list<int> lt2;
	lt2.push_back(10);
	lt2.push_back(20);
	lt2.push_back(30);
	lt2.push_back(40);

	for (auto e : lt2)
	{
		cout << e << " ";
	}
	cout << endl;

	lt1 = lt2;

	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
}